<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Model\UserTypeRule;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class WomanUserTypeRuleController extends Controller
{
    use ModelForm;

    /**
     * 用户列表
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('女用户分级推荐');
            $content->description('所有认证女用户，每日更新一次用户分级类型。');
            $content->body($this->grid());
        });
    }

    /**
     * 用户编辑
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('女用户分级推荐-编辑');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * 用户编辑
     * @return Form
     */
    protected function form()
    {
        return Admin::form(UserTypeRule::class, function (Form $form) {
            $form->display('type', '女用户类型');
            $form->text('call_min', '视频时长最小值（分钟）');
            $form->text('call_max', '视频时长最大值（分钟）');
            $form->select('recommend_type', '推荐类型')->options(UserTypeRule::$manType);

            $form->saving(function (Form $form) {
                $m = $form->model();
                $m->updated_at = date('Y-m-d H:i:s');
                $m->udpated_by = auth()->user()->username;
            });
        });
    }

    /**
     * 男用户配置
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserTypeRule());
        $where = [
            ['gender', 0]
        ];
        $grid->model()->where($where);
        $grid->type("女用户类型");
        $grid->full_call("视频分钟数")->display(function () {
            $callMin = $this->call_min;
            $callMax = $this->call_max;

            $minStr = $callMin == '' ? '无限制' : $callMin;
            $maxStr = $callMax == '' ? '无限制' : $callMax;

            return "{$minStr} --- {$maxStr}";
        });
        $grid->recommend_type("推荐类型")->display(function ($recType) {
            return UserTypeRule::$manType[$recType] ?? $recType;
        });
        $grid->udpated_by("操作人");
        $grid->updated_at("最后操作时间");

        //禁用导出
        $grid->disableExport();
        //禁用行选择
        $grid->disableRowSelector();
        //禁用新增
        $grid->disableCreation();

        $grid->actions(function ($act) {
            $act->disableDelete();
            $act->disableView();
        });

        return $grid;
    }
}
